﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.DXErrorProvider;
using MM.Common.Base;
using MM.Common.DBBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmOrganizationSetup : XtraForm
    {
        
        public event AddToMainEventHandler AddToMain;

        public FrmOrganizationSetup()
        {
            this.table = "KTSYS_ORGANIZATION";
            this.fid = "ORG_ID";
            this.InitializeComponent();
            this.cbeOrgLayer.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            this.cbeOrgLevel.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
        }
        public FrmOrganizationSetup(string rid)
        {
            this.table = "KTSYS_ORGANIZATION";
            this.fid = "ORG_ID";
            this.InitializeComponent();
            this.cbeOrgLayer.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            this.cbeOrgLevel.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            this.BindOrgLevel();
            this.BindOrgLayer();
            this.ORG_ID.Properties.ReadOnly = false;
            if (rid != null)
            {
                this.ORG_ID.Text = rid;
                string commandtext = "select * from " + this.table + " where " + this.fid + " =:dictid";
                DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("dictid", rid) });
                DataTable dt = new DataTable();
                if (SystemInfo.DbData.Retrieve(commandtext, parm, ref dt) && (dt.Rows.Count == 1))
                {
                    this.ORG_ID.Properties.ReadOnly = true;
                    this.ORG_ACTIVE.FieldValue = dt.Rows[0]["ORG_ACTIVE"].ToString();
                    this.ORG_CODE.FieldValue = dt.Rows[0]["ORG_CODE"].ToString();
                    this.ORG_IS_LEAF.FieldValue = dt.Rows[0]["ORG_IS_LEAF"].ToString();
                    this.cbeOrgLevel.EditValue = this.GetTextByValue(typeof(OrgLevel), dt.Rows[0]["ORG_LEVEL"].ToString());
                    this.cbeOrgLayer.EditValue = this.GetTextByValue(typeof(OrgLayer), dt.Rows[0]["ORG_LAYER"].ToString());
                    this.ORG_NAME.FieldValue = dt.Rows[0]["ORG_NAME"].ToString();
                    this.ORG_NAME_SHORT.FieldValue = dt.Rows[0]["ORG_NAME_SHORT"].ToString();
                    this.ORG_REMARK.FieldValue = dt.Rows[0]["ORG_REMARK"].ToString();
                    this.ORG_PARENT_ID.FieldValue = dt.Rows[0]["ORG_PARENT_ID"].ToString();
                }
            }
        }

        public FrmOrganizationSetup(string parentId, string code, string level, string layer)
        {
            this.table = "KTSYS_ORGANIZATION";
            this.fid = "ORG_ID";
            this.InitializeComponent();
            this.cbeOrgLayer.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            this.cbeOrgLevel.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            this.BindOrgLevel();
            this.BindOrgLayer();
            this.ORG_PARENT_ID.FieldValue = parentId;
            this.ORG_CODE.FieldValue = code;
            this.cbeOrgLevel.EditValue = this.GetTextByValue(typeof(OrgLevel), level);
            this.cbeOrgLayer.EditValue = this.GetTextByValue(typeof(OrgLayer), layer);
        }

        private void BindOrgLayer()
        {
            this.cbeOrgLayer.Properties.Items.Add(new OrgLayer("1", "市卫生局"));
            this.cbeOrgLayer.Properties.Items.Add(new OrgLayer("2", "区卫生局(综合医院)"));
            this.cbeOrgLayer.Properties.Items.Add(new OrgLayer("3", "社区服务中心(专科医院)"));
            this.cbeOrgLayer.Properties.Items.Add(new OrgLayer("4", "社区服务站点"));
        }

        private void BindOrgLevel()
        {
            this.cbeOrgLevel.Properties.Items.Add(new OrgLevel("1", "市卫生局"));
            this.cbeOrgLevel.Properties.Items.Add(new OrgLevel("2", "区卫生局"));
            this.cbeOrgLevel.Properties.Items.Add(new OrgLevel("3", "综合医院"));
            this.cbeOrgLevel.Properties.Items.Add(new OrgLevel("4", "专科医院(区级医院)"));
            this.cbeOrgLevel.Properties.Items.Add(new OrgLevel("5", "服务中心"));
            this.cbeOrgLevel.Properties.Items.Add(new OrgLevel("6", "服务站点"));
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.Yes;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.dxValidationProvider1.Validate())
            {
                DataParamCol parm = new DataParamCol();
                parm.Add(new InputDataParam("ORG_PARENT_ID", this.ORG_PARENT_ID.FieldValue));
                string commandtext = "select * from KTSYS_ORGANIZATION where ORG_ID=:ORG_PARENT_ID";
                DataTable dt = new DataTable();
                if ((SystemInfo.DbData.Retrieve(commandtext, parm, ref dt) && (dt != null)) && ((dt.Rows.Count > 0) && (dt.Rows[0]["ORG_IS_LEAF"].ToString() == "Y")))
                {
                    MessageBox.Show("该节点为末级节点，不能新增子节点");
                }
                else
                {
                    DataParamCol parms = new DataParamCol();
                    string str2 = "";
                    if (SystemInfo.DbData.IsExist(this.table, this.fid, this.ORG_ID.Text.Trim()))
                    {
                        string sql = string.Format("Select * From KTSYS_ORGANIZATION where ORG_CODE='{0}' and ORG_ID!={1}", this.ORG_CODE.FieldValue.ToString(), this.ORG_ID.Text.Trim());
                        if (SystemInfo.DbData.GetCount(sql, null) >= 1)
                        {
                            MessageBox.Show("该医疗机构代码已经存在，请重新输入");
                            return;
                        }
                        parms.Add(new InputDataParam("ORG_ID", this.ORG_ID.Text.Trim()));
                        parms.Add(new InputDataParam("ORG_NAME", this.ORG_NAME.FieldValue));
                        parms.Add(new InputDataParam("ORG_NAME_SHORT", this.ORG_NAME_SHORT.FieldValue));
                        parms.Add(new InputDataParam("ORG_CODE", this.ORG_CODE.FieldValue));
                        parms.Add(new InputDataParam("ORG_LEVEL", this.GetValueByText(typeof(OrgLevel), this.cbeOrgLevel.SelectedItem.ToString())));
                        parms.Add(new InputDataParam("ORG_LAYER", this.GetValueByText(typeof(OrgLayer), this.cbeOrgLayer.SelectedItem.ToString())));
                        parms.Add(new InputDataParam("ORG_PARENT_ID", this.ORG_PARENT_ID.FieldValue));
                        parms.Add(new InputDataParam("ORG_IS_LEAF", this.ORG_IS_LEAF.FieldValue));
                        parms.Add(new InputDataParam("ORG_ACTIVE", this.ORG_ACTIVE.FieldValue));
                        str2 = string.Format("Update KTSYS_ORGANIZATION Set ORG_NAME=:ORG_NAME,ORG_NAME_SHORT=:ORG_NAME_SHORT,ORG_CODE=:ORG_CODE,ORG_LEVEL=:ORG_LEVEL,ORG_LAYER=:ORG_LAYER,ORG_PARENT_ID=:ORG_PARENT_ID,ORG_IS_LEAF=:ORG_IS_LEAF,ORG_ACTIVE=:ORG_ACTIVE Where ORG_ID=:ORG_ID", new object[0]);
                    }
                    else
                    {
                        string str4 = string.Format("Select * From KTSYS_ORGANIZATION where ORG_CODE='{0}'", this.ORG_CODE.FieldValue.ToString());
                        if (SystemInfo.DbData.GetCount(str4, null) >= 1)
                        {
                            MessageBox.Show("该医疗机构代码已经存在，请重新输入");
                            return;
                        }
                        str2 = "select MAX(" + this.fid + ") from " + this.table;
                        int data = 1;
                        SystemInfo.DbData.ExecuteScalar<int>(str2, ref data);
                        this.ORG_ID.Text = (++data).ToString();
                        parms.Add(new InputDataParam("ORG_ID", this.ORG_ID.Text.Trim()));
                        parms.Add(new InputDataParam("ORG_NAME", this.ORG_NAME.FieldValue));
                        parms.Add(new InputDataParam("ORG_NAME_SHORT", this.ORG_NAME_SHORT.FieldValue));
                        parms.Add(new InputDataParam("ORG_CODE", this.ORG_CODE.FieldValue));
                        parms.Add(new InputDataParam("ORG_LEVEL", this.GetValueByText(typeof(OrgLevel), this.cbeOrgLevel.SelectedItem.ToString())));
                        parms.Add(new InputDataParam("ORG_LAYER", this.GetValueByText(typeof(OrgLayer), this.cbeOrgLayer.SelectedItem.ToString())));
                        parms.Add(new InputDataParam("ORG_PARENT_ID", this.ORG_PARENT_ID.FieldValue));
                        parms.Add(new InputDataParam("ORG_IS_LEAF", this.ORG_IS_LEAF.FieldValue));
                        parms.Add(new InputDataParam("ORG_ACTIVE", this.ORG_ACTIVE.FieldValue));
                        str2 = string.Format("insert into KTSYS_ORGANIZATION(ORG_ID, ORG_NAME, ORG_CODE, ORG_LEVEL, ORG_LAYER, ORG_PARENT_ID, ORG_IS_LEAF, ORG_ACTIVE, ORG_CREATE_DATE,ORG_NAME_SHORT) values(:ORG_ID, :ORG_NAME, :ORG_CODE, :ORG_LEVEL, :ORG_LAYER, :ORG_PARENT_ID, :ORG_IS_LEAF, :ORG_ACTIVE, SYSDATE, :ORG_NAME_SHORT)", new object[0]);
                    }
                    if (SystemInfo.DbData.IsOpen)
                    {
                        if (SystemInfo.DbData.ExecuteSql(str2, parms) == 1)
                        {
                            this.AddToMain(sender, e);
                            base.Close();
                        }
                        else
                        {
                            XtraMessageBox.Show(SystemInfo.DbData.ErrorMessage, "数据库访问错误");
                        }
                    }
                }
            }
        }
        private void FrmOrganizationSetup_Load(object sender, EventArgs e)
        {
            ConditionValidationRule rule = new ConditionValidationRule
            {
                ConditionOperator = ConditionOperator.IsNotBlank,
                ErrorText = "该值不能为空！"
            };
            this.dxValidationProvider1.SetValidationRule(this.ORG_NAME, rule);
            this.dxValidationProvider1.SetValidationRule(this.ORG_CODE, rule);
        }

        private string GetTextByValue(Type type, string OrgTypeValue)
        {
            if (type == typeof(OrgLevel))
            {
                foreach (OrgLevel level in this.cbeOrgLevel.Properties.Items)
                {
                    if (level.OrgLevelNum == OrgTypeValue)
                    {
                        return level.OrgLevelName;
                    }
                }
            }
            else if (type == typeof(OrgLayer))
            {
                foreach (OrgLayer layer in this.cbeOrgLayer.Properties.Items)
                {
                    if (layer.OrgLayerNum == OrgTypeValue)
                    {
                        return layer.OrgLayerName;
                    }
                }
            }
            return "未知";
        }

        private string GetValueByText(Type type, string OrgTypeText)
        {
            if (type == typeof(OrgLevel))
            {
                foreach (OrgLevel level in this.cbeOrgLevel.Properties.Items)
                {
                    if (level.OrgLevelName == OrgTypeText)
                    {
                        return level.OrgLevelNum;
                    }
                }
            }
            else if (type == typeof(OrgLayer))
            {
                foreach (OrgLayer layer in this.cbeOrgLayer.Properties.Items)
                {
                    if (layer.OrgLayerName == OrgTypeText)
                    {
                        return layer.OrgLayerNum;
                    }
                }
            }
            return "未知";
        }
        // Nested Types
        public delegate void AddToMainEventHandler(object sender, EventArgs e);

        private class OrgLayer
        {
            // Fields
            private string _OrgLayerName;
            private string _OrgLayerNum;

            // Methods
            public OrgLayer(string OrgLayerNum, string OrgLayerName)
            {
                this._OrgLayerNum = OrgLayerNum;
                this._OrgLayerName = OrgLayerName;
            }

            public override string ToString()
            {
                return this._OrgLayerName;
            }

            // Properties
            public string OrgLayerName
            {
                get
                {
                    return this._OrgLayerName;
                }
                set
                {
                    this._OrgLayerName = value;
                }
            }

            public string OrgLayerNum
            {
                get
                {
                    return this._OrgLayerNum;
                }
                set
                {
                    this._OrgLayerNum = value;
                }
            }
        }

        private class OrgLevel
        {
            // Fields
            private string _OrgLevelName;
            private string _OrgLevelNum;

            // Methods
            public OrgLevel(string OrgLevelNum, string OrgLevelName)
            {
                this._OrgLevelNum = OrgLevelNum;
                this._OrgLevelName = OrgLevelName;
            }

            public override string ToString()
            {
                return this._OrgLevelName;
            }

            // Properties
            public string OrgLevelName
            {
                get
                {
                    return this._OrgLevelName;
                }
                set
                {
                    this._OrgLevelName = value;
                }
            }

            public string OrgLevelNum
            {
                get
                {
                    return this._OrgLevelNum;
                }
                set
                {
                    this._OrgLevelNum = value;
                }
            }
        }
    }
}
